// 代理地址 [head 代理头] [local 本地] [network 网络]
// const server = 'https://sltrczx.scslrc.cn'

const server = 'http://124.71.132.153:80'
const proxyPathMap = {
  apiC: {
    local: server + '/website',
    network: '/website'
  },
  apiFile: {
    local: server + '/file',
    network: '/file'
  },
  // 业务接口-陈智睿
  apiWorkCZR: {
    // local: local,
    local: server + '/website',
    network: '/website'
  },
  // 登录接口-陈智睿
  loginAuth: {
    local: server + '/website',
    network: '/website'
  },
  dataDictionary: {
    local: server + '/website',
    network: '/website'
  }
}

/**
 * 获取代理地址
 * @param {string} key
 * @returns
 */
function getProxyPath(key) {
  const path = proxyPathMap[key]
  if (!path) {
    console.warn('未找到对应的地址配置')
    return ''
  }
  const env = process.env.NODE_ENV
  if (env === 'development') {
    return `/${key}`
  } else {
    return path.network
  }
}

/**根据不同环境创建代理 */
function createProxy() {
  const flag = process.env.VUE_APP_FLAG
  const isNetwork = flag === 'development_network'
  if (isNetwork) {
    console.log('\x1B[33m%s\x1b[0m:', '[注意: 线上开发环境连接的是正式环境的接口地址， 请谨慎操作!]')
  }
  let proxy = {}
  for (let key in proxyPathMap) {
    const item = proxyPathMap[key]
    const target = isNetwork ? item.network : item.local
    proxy[`/${key}`] = {
      target,
      changeOrigin: true,
      pathRewrite: { [`^/${key}`]: '' }
    }
  }
  return proxy
}

module.exports = {
  server,
  getProxyPath,
  createProxy
}
